package com.ovo.dailypayaccount.utils;

import com.ovo.dailypayaccount.entity.PayData;
import com.ovo.dailypayaccount.entity.PayDataClassify;
import net.sf.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 消费数据处理
 * @Author wubin
 * @Date 2019-04-05 20:20
 * @Version 1.0
 **/
public class PayDataHandleUtil {
    /**
    *@Description 将多条数据转化为分类数据汇总
    *@Author wubin
    *@Date 2019-04-05 20:22
    *@Param [list]
    *@Return java.util.List<com.ovo.dailypayaccount.entity.PayDataClassify>
    @Exception 异常说明
    **/
    public static List<PayDataClassify> getClassifyPayData(List<PayData>list) {
        List<PayDataClassify> pieData =new ArrayList<>();
        //获取分类消费数
        BigDecimal breakfast = new BigDecimal(0);
        BigDecimal lunch = new BigDecimal(0);
        BigDecimal supper = new BigDecimal(0);
        BigDecimal purchase = new BigDecimal(0);
        BigDecimal triffic = new BigDecimal(0);
        BigDecimal others = new BigDecimal(0);
        for(PayData pd:list) {
            //处理分类数据
            String payItems = pd.getPayItems();
            JSONObject ob = JSONObject.fromObject(payItems);
            if(ob.containsKey("breakfast")) {
                String breakfast2 = ob.getString("breakfast");
                BigDecimal breakfast3 = BigDecimal.valueOf(Double.valueOf(breakfast2));
                breakfast = breakfast.add(breakfast3);
            }
            if(ob.containsKey("lunch")) {
                String lunch2 = ob.getString("lunch");
                BigDecimal lunch3 = BigDecimal.valueOf(Double.valueOf(lunch2));
                lunch = lunch.add(lunch3);
            }
            if(ob.containsKey("supper")) {
                String supper2 = ob.getString("supper");
                BigDecimal supper3 = BigDecimal.valueOf(Double.valueOf(supper2));
                supper = supper.add(supper3);
            }
            if(ob.containsKey("purchase")) {
                String purchase2 = ob.getString("purchase");
                BigDecimal purchase3 = BigDecimal.valueOf(Double.valueOf(purchase2));
                purchase = purchase.add(purchase3);
            }
            if(ob.containsKey("traffic")) {
                String triffic2 = ob.getString("traffic");
                BigDecimal triffic3 = BigDecimal.valueOf(Double.valueOf(triffic2));
                triffic = triffic.add(triffic3);
            }
            if(ob.containsKey("others")) {
                String others2 = ob.getString("others");
                BigDecimal others3 = BigDecimal.valueOf(Double.valueOf(others2));
                others = others.add(others3);
            }

        }
        PayDataClassify breakfast4 = new PayDataClassify();
        breakfast4.setItem("早餐");
        breakfast4.setAmount(breakfast.toString());
        pieData.add(breakfast4);
        PayDataClassify lunch4 = new PayDataClassify();
        lunch4.setItem("午餐");
        lunch4.setAmount(lunch.toString());
        pieData.add(lunch4);
        PayDataClassify supper4 = new PayDataClassify();
        supper4.setItem("晚餐");
        supper4.setAmount(supper.toString());
        pieData.add(supper4);
        PayDataClassify purchase4 = new PayDataClassify();
        purchase4.setItem("购物");
        purchase4.setAmount(purchase.toString());
        pieData.add(purchase4);
        PayDataClassify triffic4 = new PayDataClassify();
        triffic4.setItem("交通");
        triffic4.setAmount(triffic.toString());
        pieData.add(triffic4);
        PayDataClassify others4 = new PayDataClassify();
        others4.setItem("其他");
        others4.setAmount(others.toString());
        pieData.add(others4);
        return pieData;
    }
}
